//
//  DTCNavigationBarView.m
//  DemoTest
//
//  Created by JianWei Chen on 17/3/11.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "DTCNavigationBarView.h"

#define DTCNavigationBarHeight 44
#define DTCNavigationStatusBarHeight 20
#define DTCNavigationBarWidth [UIScreen mainScreen].bounds.size.width
#define DTCNavigationHeight (DTCNavigationBarHeight + DTCNavigationStatusBarHeight)
#define DTCItemSpaceWidth 10.f
//#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125,2436), [[UIScreen mainScreen] currentMode].size) : NO)
//#define DTCNavigationHeight iPhoneX ? 84 : 64
//#define DTCNavigationBarHeight 44
//#define DTCNavigationStatusBarHeight iPhoneX ? 40 : 20

@interface DTCNavigationBarView ()

@property (nonatomic, strong) UIImageView *backgroundImageView;//背景图片
@property (nonatomic, strong) UIView *navigationBar;
@property (nonatomic, strong) UIView *statusBar;
@property (nonatomic, strong) UIView *contentTitleView;
@property (nonatomic, strong) UIView *leftBarButtonView;
@property (nonatomic, strong) UIView *rightBarButtonView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation DTCNavigationBarView
+(DTCNavigationBarView *)getNavigationView:(UIView *)superView
{
    DTCNavigationBarView *view = [[DTCNavigationBarView alloc]initWithFrame:CGRectMake(0, 0, DTCNavigationBarWidth, DTCNavigationHeight)];
    [superView addSubview:view];
    return view;
}

- (void)setBackgroundImage:(UIImage *)image
{
    self.backgroundImageView.image = image;
}

- (void)setTitle:(NSString *)title textColor:(UIColor *)textColor
{
    self.titleLabel.text = title;
    self.titleLabel.textColor = textColor;
    self.titleLabel.hidden = NO;
    
    for (UIView *view in self.contentTitleView.subviews) {
        if ([view isEqual:self.titleLabel]) {
            return;
        }
    }
    [self.contentTitleView addSubview:self.titleLabel];
}

- (void)setTitleView:(UIView *)titleView
{
    for (UIView *view in self.contentTitleView.subviews) {
        [view removeFromSuperview];
    }
    titleView.center = CGPointMake(CGRectGetWidth(self.contentTitleView.bounds)/2.f, CGRectGetHeight(self.contentTitleView.bounds)/2.f);
    [self.contentTitleView addSubview:titleView];
}

- (void)setLeftBarButtonItem:(UIView *)leftBarButtonItem
{
    for (UIView *view in self.leftBarButtonView.subviews) {
        [view removeFromSuperview];
    }
    
    leftBarButtonItem.frame = CGRectMake(DTCItemSpaceWidth, 0,CGRectGetWidth(leftBarButtonItem.bounds) , CGRectGetHeight(leftBarButtonItem.bounds));
    leftBarButtonItem.center = CGPointMake(leftBarButtonItem.center.x, CGRectGetHeight(self.leftBarButtonView.bounds)/2.f);
    [self.leftBarButtonView addSubview:leftBarButtonItem];
}

- (void)setLeftBarButtonItems:(NSArray<UIView *> *)leftBarButtonItems
{
    for (UIView *view in self.leftBarButtonView.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat x = 0;
    for (int i = 0; i < leftBarButtonItems.count; i++) {
        x += DTCItemSpaceWidth;
        
        UIView *leftBarButtonItem = leftBarButtonItems[i];
        leftBarButtonItem.frame = CGRectMake(x, 0,CGRectGetWidth(leftBarButtonItem.bounds) , CGRectGetHeight(leftBarButtonItem.bounds));
        leftBarButtonItem.center = CGPointMake(leftBarButtonItem.center.x, CGRectGetHeight(self.leftBarButtonView.bounds)/2.f);
        [self.leftBarButtonView addSubview:leftBarButtonItem];
        
        x += CGRectGetWidth(leftBarButtonItem.bounds);
    }
}

- (void)setRightBarButtonItem:(UIView *)rightBarButtonItem
{
    for (UIView *view in self.rightBarButtonView.subviews) {
        [view removeFromSuperview];
    }
    
    rightBarButtonItem.frame = CGRectMake(CGRectGetWidth(self.rightBarButtonView.bounds) - CGRectGetWidth(rightBarButtonItem.bounds) - DTCItemSpaceWidth, 0,CGRectGetWidth(rightBarButtonItem.bounds) , CGRectGetHeight(rightBarButtonItem.bounds));
    rightBarButtonItem.center = CGPointMake(rightBarButtonItem.center.x, CGRectGetHeight(self.rightBarButtonView.bounds)/2.f);
    [self.rightBarButtonView addSubview:rightBarButtonItem];
}

- (void)setRightBarButtonItems:(NSArray<UIView *> *)rightBarButtonItems
{
    for (UIView *view in self.rightBarButtonView.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat x = 0;
    for (int i = 0; i < rightBarButtonItems.count; i++) {
        x += DTCItemSpaceWidth;
        
        UIView *rightBarButtonItem = rightBarButtonItems[i];
        rightBarButtonItem.frame = CGRectMake(CGRectGetWidth(self.rightBarButtonView.bounds) - CGRectGetWidth(rightBarButtonItem.bounds) - x, 0,CGRectGetWidth(rightBarButtonItem.bounds) , CGRectGetHeight(rightBarButtonItem.bounds));
        rightBarButtonItem.center = CGPointMake(rightBarButtonItem.center.x, CGRectGetHeight(self.rightBarButtonView.bounds)/2.f);
        [self.rightBarButtonView addSubview:rightBarButtonItem];
        
        x += CGRectGetWidth(rightBarButtonItem.bounds);
    }
}

- (void)setNavigationBarBackgroundColor:(UIColor *)backgroundColor
{
    self.navigationBar.backgroundColor = backgroundColor;
}

- (void)setStatusBarBackgroundColor:(UIColor *)statusBackgroundColor
{
    self.statusBar.backgroundColor = statusBackgroundColor;
}

- (void)setLineViewHidden:(BOOL)hidden
{
    self.lineView.hidden = hidden;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        [self backgroundImageView];
        [self statusBar];
        [self navigationBar];
        [self leftBarButtonView];
        [self rightBarButtonView];
        [self contentTitleView];
        [self lineView];
    }
    return self;
}
- (UIImageView *)backgroundImageView
{
    @synchronized (_backgroundImageView) {
        if (!_backgroundImageView) {
            _backgroundImageView = [[UIImageView alloc]initWithFrame:self.bounds];
            _backgroundImageView.contentMode = UIViewContentModeScaleToFill;
            [self addSubview:_backgroundImageView];
        }
    }
    return _backgroundImageView;
}

- (UIView *)navigationBar
{
    @synchronized (_navigationBar) {
        if (!_navigationBar) {
            _navigationBar = [[UIView alloc]initWithFrame:CGRectMake(0, DTCNavigationStatusBarHeight, DTCNavigationBarWidth, DTCNavigationBarHeight)];
            [self addSubview:_navigationBar];
        }
    }
    return _navigationBar;
}

- (UIView *)statusBar
{
    @synchronized (_statusBar) {
        if (!_statusBar) {
            _statusBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DTCNavigationBarWidth, DTCNavigationStatusBarHeight)];
            [self addSubview:_statusBar];
        }
    }
    return _statusBar;
}

- (UIView *)contentTitleView
{
    @synchronized (_contentTitleView) {
        if (!_contentTitleView) {
            _contentTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DTCNavigationBarWidth/3.f, DTCNavigationBarHeight)];
            _contentTitleView.center = CGPointMake(DTCNavigationBarWidth/2.f, DTCNavigationBarHeight/2.f);
            [self.navigationBar addSubview:_contentTitleView];
        }
    }
    return _contentTitleView;
}

- (UIView *)leftBarButtonView
{
    @synchronized (_leftBarButtonView) {
        if (!_leftBarButtonView) {
            _leftBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DTCNavigationBarWidth/3.f, DTCNavigationBarHeight)];
            [self.navigationBar addSubview:_leftBarButtonView];
        }
    }
    return _leftBarButtonView;
}

- (UIView *)rightBarButtonView
{
    @synchronized (_rightBarButtonView) {
        if (!_rightBarButtonView) {
            _rightBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(DTCNavigationBarWidth*2.f/3.f, 0, DTCNavigationBarWidth/3.f, DTCNavigationBarHeight)];
            [self.navigationBar addSubview:_rightBarButtonView];
        }
    }
    return _rightBarButtonView;
}

- (UILabel *)titleLabel
{
    @synchronized (_titleLabel) {
        if (!_titleLabel) {
            _titleLabel = [[UILabel alloc]initWithFrame:self.contentTitleView.bounds];
            _titleLabel.textColor = [UIColor blackColor];
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.font = [UIFont systemFontOfSize:17.f];
        }
    }
    return _titleLabel;
}

- (UIView *)lineView
{
    @synchronized (_lineView) {
        if (!_lineView) {
            _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, DTCNavigationStatusBarHeight + DTCNavigationBarHeight, DTCNavigationBarWidth, 0.3)];
            _lineView.backgroundColor = [UIColor lightGrayColor];
            _lineView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
            _lineView.layer.shadowOffset = CGSizeMake(DTCNavigationBarWidth, 5);
            _lineView.layer.shadowOpacity = 5;
            [self addSubview:_lineView];
        }
    }
    return _lineView;
}
@end
